package com.hmjk.health.views;

import com.hmjk.health.R;

import android.content.Context;
import android.content.res.TypedArray;
import android.util.AttributeSet;
import android.util.TypedValue;
import android.view.Gravity;
import android.view.View.MeasureSpec;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.TextView;

public class CakyLayout extends RelativeLayout{
	private int mtextColor = 0xff444444;	
    private Context mContext;
    private TextView mTopTxtView, mLeftTxtView, mRightTxtView, mCenterTxtView;
    private ImageView mLeftImgView,mRightImgView;
    private CakyView mCacy;
    
	public CakyLayout(Context context) {
		super(context);
		// TODO Auto-generated constructor stub
		mContext = context;
		initSubViews();
	}
	
	public CakyLayout(Context context, AttributeSet attrs) {
		super(context, attrs);
		// TODO Auto-generated constructor stub
		mContext = context;
		init(context,attrs);
		initSubViews();
	}	
	
	public CakyLayout(Context context, AttributeSet attrs, int defStyle) {
		super(context, attrs, defStyle);
		// TODO Auto-generated constructor stub
		mContext = context;
		init(context,attrs);
		initSubViews();
	}
	
	private float mtextSize = 0;
	private float mCacyHeight = 0;

	private void init(Context context, AttributeSet attrs)
	{
		if(null != attrs){
			TypedArray a = context.obtainStyledAttributes(attrs, R.styleable.CakyLayout);
			int num = a.getIndexCount();
			for(int i=0; i<num; i++){
				int attr = a.getIndex(i);
				switch (attr) {
					case R.styleable.CakyLayout_TextSize:
						mtextSize = a.getDimensionPixelSize(attr, (int) mtextSize);
					break;
					case R.styleable.CakyLayout_CakyHeight:
						mCacyHeight = a.getDimensionPixelSize(attr,(int) mCacyHeight);
					break;
				}
			}
			
			a.recycle();
		}
		
	}
	
	private void initSubViews()
	{
	    mTopTxtView = new TextView(mContext);
	    RelativeLayout.LayoutParams lp = new RelativeLayout.LayoutParams(
	    LayoutParams.MATCH_PARENT,LayoutParams.WRAP_CONTENT);
	    lp.addRule(RelativeLayout.ALIGN_PARENT_TOP);//与父容器的上侧对齐
	    lp.topMargin = margin;	    
	    mTopTxtView.setLayoutParams(lp);//设置布局参数
	    mTopTxtView.setTextSize(TypedValue.COMPLEX_UNIT_PX,mtextSize);
	    mTopTxtView.setId(1);
	    mTopTxtView.setGravity(Gravity.CENTER_HORIZONTAL);
	    mTopTxtView.setTextColor(mtextColor);
	    addView(mTopTxtView);//RelativeLayout添加子View
	    
	    RelativeLayout cenLayout = new RelativeLayout(mContext);
	    RelativeLayout.LayoutParams lp1 = new RelativeLayout.LayoutParams(
	    LayoutParams.WRAP_CONTENT,LayoutParams.WRAP_CONTENT);	    
	    lp1.addRule(RelativeLayout.BELOW,mTopTxtView.getId());
	    lp1.addRule(RelativeLayout.CENTER_HORIZONTAL);
	    lp1.topMargin = margin;
	    lp1.bottomMargin = margin;
	    cenLayout.setLayoutParams(lp1);
	    cenLayout.setId(2);
	    addView(cenLayout);
   	    
	    mCacy = new CakyView(mContext);
	    RelativeLayout.LayoutParams lp2 = new RelativeLayout.LayoutParams(
	    LayoutParams.WRAP_CONTENT,LayoutParams.WRAP_CONTENT);	    
	    lp2.addRule(RelativeLayout.CENTER_IN_PARENT);
	    mCacy.setHeight((int) mCacyHeight);
	    mCacy.setLayoutParams(lp);
	    mCacy.setId(7);
	    cenLayout.addView(mCacy);//RelativeLayout添加子View
	    
	    mCenterTxtView = new TextView(mContext);
	    RelativeLayout.LayoutParams lp8 = new RelativeLayout.LayoutParams(
	    (int) (mCacyHeight/3),(int) (mCacyHeight/3));	
	    lp8.addRule(RelativeLayout.CENTER_IN_PARENT);
	    mCenterTxtView.setGravity(Gravity.CENTER);
	    mCenterTxtView.setTextSize(TypedValue.COMPLEX_UNIT_PX,mtextSize);
	    mCenterTxtView.setId(8);
	    mCenterTxtView.setLayoutParams(lp8);
	    mCenterTxtView.setTextColor(mtextColor);
	    cenLayout.addView(mCenterTxtView);
	    
	    
	    mLeftImgView = new ImageView(mContext);
	    mLeftImgView.setImageResource(R.drawable.health_toolow);
	    RelativeLayout.LayoutParams lp3 = new RelativeLayout.LayoutParams(
	    LayoutParams.WRAP_CONTENT,LayoutParams.WRAP_CONTENT);	
	    lp3.addRule(RelativeLayout.ALIGN_PARENT_LEFT);
	    lp3.addRule(RelativeLayout.ALIGN_PARENT_BOTTOM);
	    lp3.leftMargin = 20;
	    lp3.bottomMargin = margin;
	    mLeftImgView.setLayoutParams(lp3);
	    mLeftImgView.setId(3);
	    addView(mLeftImgView);
	    
	    mLeftTxtView = new TextView(mContext);
	    RelativeLayout.LayoutParams lp4 = new RelativeLayout.LayoutParams(
	    LayoutParams.WRAP_CONTENT,LayoutParams.WRAP_CONTENT);	
	    lp4.addRule(RelativeLayout.RIGHT_OF,mLeftImgView.getId());
	    lp4.addRule(RelativeLayout.ALIGN_PARENT_BOTTOM);
	    lp4.leftMargin = margin;
	    lp4.bottomMargin = margin;
	    mLeftTxtView.setTextSize(TypedValue.COMPLEX_UNIT_PX,mtextSize);
	    mLeftTxtView.setLayoutParams(lp4);
	    mLeftTxtView.setId(4);
	    mLeftTxtView.setTextColor(mtextColor);
	    addView(mLeftTxtView);	    
	    
	    mRightImgView = new ImageView(mContext);
	    mRightImgView.setImageResource(R.drawable.health_toohigh);
	    RelativeLayout.LayoutParams lp5 = new RelativeLayout.LayoutParams(
	    LayoutParams.WRAP_CONTENT,LayoutParams.WRAP_CONTENT);	
	    lp5.addRule(RelativeLayout.ALIGN_PARENT_RIGHT);
	    lp5.addRule(RelativeLayout.ALIGN_PARENT_BOTTOM);
	    lp5.rightMargin = 30;
	    lp5.bottomMargin = margin;
	    mRightImgView.setLayoutParams(lp5);
	    mRightImgView.setId(5);
	    addView(mRightImgView);
	    
	    mRightTxtView = new TextView(mContext);
	    RelativeLayout.LayoutParams lp6 = new RelativeLayout.LayoutParams(
	    LayoutParams.WRAP_CONTENT,LayoutParams.WRAP_CONTENT);	
	    lp6.addRule(RelativeLayout.LEFT_OF,mRightImgView.getId());
	    lp6.addRule(RelativeLayout.ALIGN_PARENT_BOTTOM);
	    lp6.rightMargin = margin;
	    lp6.bottomMargin = margin;
	    mRightTxtView.setTextSize(TypedValue.COMPLEX_UNIT_PX,mtextSize);
	    mRightTxtView.setLayoutParams(lp6);
	    mRightTxtView.setId(6);
	    mRightTxtView.setTextColor(mtextColor);
	    addView(mRightTxtView);		    	    
	}
	
	public void setParam(int total, int normal, int below, int higher)
	{
		mCacy.setParam(total, normal, below, higher);
		String s = String.format(mContext.getResources().getString(R.string.measure_normal_number), normal);
		mTopTxtView.setText(s);
		
		s = String.format(mContext.getResources().getString(R.string.measure_total_number), total);
		mCenterTxtView.setText(s);

		s = String.format(mContext.getResources().getString(R.string.measure_low_number), below);
		mLeftTxtView.setText(s);
		
		s = String.format(mContext.getResources().getString(R.string.measure_high_number), higher);
		mRightTxtView.setText(s);		
	}
	
	private static int margin = 4;

	@Override
	protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec){
		
		int widthMode = MeasureSpec.getMode(widthMeasureSpec);
		int widthSize = MeasureSpec.getSize(widthMeasureSpec);	
		int heightMode = MeasureSpec.getMode(heightMeasureSpec);
		int heightSize = MeasureSpec.getSize(heightMeasureSpec);		

		heightSize = (int) (mtextSize + mCacyHeight +8*margin);

		int hSpec = MeasureSpec.makeMeasureSpec(heightSize,heightMode);		
		super.onMeasure(widthMeasureSpec, hSpec);
	}
	
	@Override
	protected void onLayout(boolean changed, int l, int t, int r, int b) {
		super.onLayout(changed, l, t, r, b);
		
	}
}